package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/8/17 14:44
 * https://leetcode.cn/problems/watering-plants-ii/
 */
public class Title2105 {

    public static int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int n = plants.length;
        int left = 0, right = n - 1;
        int curA = capacityA;
        int curB = capacityB;
        int count = 0;
        while (left <= right) {
            if (left == right) {
                //left == right
                int max = Math.max(curA, curB);
                if (max < plants[left]) {
                    count++;
                }
                break;
            }
            if (curA < plants[left]) {
                //重新取水
                count++;
                curA = capacityA;
            }
            curA -= plants[left];
            left++;
            if (curB < plants[right]) {
                count++;
                curB = capacityB;
            }
            curB -= plants[right];
            right--;
        }
        return count;
    }

    public static void main(String[] args) {

    }
}
